//
//  HomeViewController.m
//  PhotographyStudi
//
//  Created by 叶一枫 on 2017/5/16.
//  Copyright © 2017年 叶一枫. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@property (nonatomic,strong)UICollectionViewLayout *layout;

@property (nonatomic,strong)NSMutableArray *heightArr;

@property (weak, nonatomic) IBOutlet BaseCollectionView *collectionView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.collectionViewLayout = self.layout;
    self.collectionView.alwaysBounceVertical = YES;
    WEAKSELF
    self.collectionView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf.collectionView.mj_header endRefreshing];
    }];
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [weakSelf.collectionView.mj_footer endRefreshing];
    }];
}




-(NSMutableArray *)heightArr{
    if (!_heightArr) {
        //随机生成高度
        NSMutableArray *arr = [NSMutableArray array];
        for (int i=0; i<12; i++) {
            [arr addObject:@(arc4random()%50+140)];
        }
        _heightArr = [arr copy];
    }
    return _heightArr;
}

-(UICollectionViewLayout *)layout{
    if (!_layout) {
        _layout = [[BSCollectionViewLayout alloc]initWithItemsHeightBlock:^CGFloat(NSIndexPath *indexPath) {
            return [self.heightArr[indexPath.item]floatValue];
        } marginX:8 colCount:2];
    }

    return _layout;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.heightArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fallsCell" forIndexPath:indexPath];
    cell.tag = indexPath.row;
    cell.imageView.backgroundColor = [UIColor greenColor];
    [cell addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(checkHighQuality:)]];
    return cell;
}

-(void)checkHighQuality:(UILongPressGestureRecognizer*)tap{

    if (tap.state == UIGestureRecognizerStateBegan) {
        HomeCollectionCell *cell = (HomeCollectionCell*)tap.view;
        cell.imageView = nil;
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
